package com.cn.web.controller.TechnologicalStandard;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.TechnologicalStandard.GrindingProcessStandardMain;
import com.cn.bean.pojo.TechnologicalStandard.ProductRollerRoughnessSettingMain;
import com.cn.bean.pojo.TechnologicalStandard.ProductRollerRoughnessSettingSub;
import com.cn.bean.pojo.TechnologicalStandard.ReplacementCategory;
import com.cn.bean.pojo.bc.User;
import com.cn.config.config.Result;
import com.cn.service.server.TechnologicalStandard.ProductRollerRoughnessSettingMainService;
import com.cn.service.server.TechnologicalStandard.ReplaceProcessStandardsService;
import com.cn.service.server.bc.MaterialServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Prod")
@Transactional
public class ProductRollerRoughnessSettingMainController {

    private final ProductRollerRoughnessSettingMainService replaceProcessStandardsService;
    private final MaterialServer materialServer;

    @Autowired(required = false)
    public ProductRollerRoughnessSettingMainController(MaterialServer materialServer, ProductRollerRoughnessSettingMainService replaceProcessStandardsService) {
        this.replaceProcessStandardsService = replaceProcessStandardsService;
        this.materialServer = materialServer;
    }
    @RequestMapping("/select/ReplaceProcessStandards")
    @ResponseBody
    public List<ProductRollerRoughnessSettingMain> selectReplaceProcessStandards(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String equipmentStructure = jsonObject.getString("equipmentStructure");
        equipmentStructure = (equipmentStructure != null) ? equipmentStructure : "";
        List<ProductRollerRoughnessSettingMain> grindingProcessStandardMains = replaceProcessStandardsService.selectReplaceProcessStandards(page, limit, equipmentStructure);
        for (ProductRollerRoughnessSettingMain grindingProcessStandardMain : grindingProcessStandardMains) {
            if (grindingProcessStandardMain.getState() == 0) {
                grindingProcessStandardMain.setStateName("待审核");

            } else {
                grindingProcessStandardMain.setStateName("已审核");
            }

            switch (grindingProcessStandardMain.getProcessProperty()) {

                case 1: {
                    grindingProcessStandardMain.setProcessPropertyName("成品");
                    break;
                }
                case 2: {
                    grindingProcessStandardMain.setProcessPropertyName("半成品");
                    break;
                }
                case 5: {
                    grindingProcessStandardMain.setProcessPropertyName("成品,半成品");
                    break;
                }
            }

            switch (grindingProcessStandardMain.getProcedures()) {
                case 0: {
                    grindingProcessStandardMain.setProceduresName("轧机");
                    break;
                }
                case 1: {
                    grindingProcessStandardMain.setProceduresName("退火");
                    break;
                }
                case 2: {
                    grindingProcessStandardMain.setProceduresName("拉矫");
                    break;
                }
                case 3: {
                    grindingProcessStandardMain.setProceduresName("分条");
                    break;
                }
                case 4: {
                    grindingProcessStandardMain.setProceduresName("清洗线");
                    break;
                }
                case 5: {
                    grindingProcessStandardMain.setProceduresName("破带");
                    break;
                }
                case 6: {
                    grindingProcessStandardMain.setProceduresName("大分条");
                    break;
                }
                case 7: {
                    grindingProcessStandardMain.setProceduresName("去应力");
                    break;
                }
                case 8: {
                    grindingProcessStandardMain.setProceduresName("贴膜");
                    break;
                }
                case 9: {
                    grindingProcessStandardMain.setProceduresName("研磨");
                    break;
                }
            }

        }
        return grindingProcessStandardMains;
    }

    @RequestMapping("/add/ReplaceProcessStandards")
    @ResponseBody
    public Result addReplaceProcessStandards(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        String creationTime = jsonObject.getString("creationTime");
        creationTime = creationTime.replace("/", "-");
        ProductRollerRoughnessSettingMain formMainTable = jsonObject.getObject("formMainTable", ProductRollerRoughnessSettingMain.class);
        List<User> users = materialServer.selectUser(userName);
        String name = users.get(0).getName();
        formMainTable.setCreationTime(creationTime);
        formMainTable.setCreator(name);
        formMainTable.setCreatorID(userName);

        //更换类别
        JSONArray tableDate3Lsit = jsonObject.getJSONArray("childList");
        List<ProductRollerRoughnessSettingSub> replacementCategoryList = tableDate3Lsit.toJavaList(ProductRollerRoughnessSettingSub.class);

        return replaceProcessStandardsService.addReplaceProcessStandards(formMainTable, replacementCategoryList);
    }
    @RequestMapping("/select/ReplaceProcessStandardsId/Id")
    @ResponseBody
    public List<ProductRollerRoughnessSettingMain> selectReplaceProcessStandardsId(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        return replaceProcessStandardsService.selectReplaceProcessStandardsId(id);
    }
    @RequestMapping("/delete/GrindingProcessStandardMain")
    @ResponseBody
    public Result deleteGrindingProcessStandardMain(@RequestBody JSONObject jsonObject) {
        JSONArray deleteIdList = jsonObject.getJSONArray("deleteIdList");
        List<ProductRollerRoughnessSettingMain> SpecializedGrindingEquipmentSetup = deleteIdList.toJavaList(ProductRollerRoughnessSettingMain.class);

        return replaceProcessStandardsService.deleteGrindingProcessStandardMain(SpecializedGrindingEquipmentSetup);
    }
    @RequestMapping("/update/GrindingProcessStandardMain")
    @ResponseBody
    public Result updateGrindingProcessStandardMain(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        String creationTime = jsonObject.getString("creationTime");
        creationTime = creationTime.replace("/", "-");
        ProductRollerRoughnessSettingMain formMainTable = jsonObject.getObject("formMainTable", ProductRollerRoughnessSettingMain.class);
        List<User> users = materialServer.selectUser(userName);
        String name = users.get(0).getName();
        formMainTable.setModifierID(userName);
        formMainTable.setModifier(name);
        formMainTable.setModificationTime(creationTime);
        //更换类别
        JSONArray tableDate3Lsit = jsonObject.getJSONArray("childList");
        List<ProductRollerRoughnessSettingSub> replacementCategoryList = tableDate3Lsit.toJavaList(ProductRollerRoughnessSettingSub.class);

        return replaceProcessStandardsService.updateGrindingProcessStandardMain(formMainTable,replacementCategoryList);
    }
    @RequestMapping("/update/Sh/GrindingProcessStandardMain")
    @ResponseBody
    public Result updateShGrindingProcessStandardMain(@RequestBody JSONObject jsonObject) {
        JSONArray deleteIdList = jsonObject.getJSONArray("deleteIdList");
        List<ProductRollerRoughnessSettingMain> SpecializedGrindingEquipmentSetup = deleteIdList.toJavaList(ProductRollerRoughnessSettingMain.class);
        String userName = jsonObject.getString("userName");
        String creationTime = jsonObject.getString("creationTime");
        creationTime = creationTime.replace("/", "-");
        List<User> users = materialServer.selectUser(userName);
        String name = users.get(0).getName();
        Integer state = jsonObject.getInteger("state");
        return replaceProcessStandardsService.updateShGrindingProcessStandardMain(SpecializedGrindingEquipmentSetup, state, name, userName, creationTime);
    }


}
